package com.isprint.jslx.routes;

import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.kabao.ext.kit.JaxbKit;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ydw on 2017/7/14.
 */
public class RoutesProperties {

    public static Map<String, String> ROUTES = new HashMap<String, String>();

    public static void setROUTES(Routes me){
        File[] files = new File(RoutesProperties.class.getClassLoader().getResource("").getFile()).listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().endsWith("route.xml");
            }
        });
        String type;
        String pkge;
        try {
            for (File xmlfile : files) {
                RouteGroup group = (RouteGroup) JaxbKit.unmarshal(xmlfile, RouteGroup.class);
                type = group.type;
                pkge = group.pkge;
                RoutesProperties.ROUTES.put(pkge, ("".equals(type) ? "" : type + "/"));
                for (RouteItem routeItem : group.routeItems) {
                    me.add(type + routeItem.controllerkey, (Class<? extends Controller>) Class.forName(pkge + "." + routeItem.className));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
